<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Staff extends MY_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function __construct() {
        parent::__construct();
        $this->per_page = 1;
        $this->load->model('general_model');
    }

    public function index() {
        parent::core_staff_view('staff/body_view');
    }

    public function add_patient() {
        $data['pat_right'] = $this->general_model->get_field('distinct(pat_right)', 'patients');
        $data['pat_nationality'] = $this->general_model->get_field('distinct(pat_nationality)', 'patients');
        $data['pat_race'] = $this->general_model->get_field('distinct(pat_race)', 'patients');
        $data['pat_raligion'] = $this->general_model->get_field('distinct(pat_raligion)', 'patients');
        $data['amp_types'] = $this->general_model->get_field('*', 'amp_types');

        $post = $this->input->post();
        $data['patient'][] = $post;
        parent::core_staff_view('staff/add_patient', $data);
    }

    public function query() {

        $check_null = array('road', 'street', 'c_road', 'c_treet', 'pat_tel_local', 'pat_mobile_local', 'pat_tel_current', 'pat_mobile_current');
        foreach ($check_null as $key => $value) {

            if ($this->input->post($value) == '') {
                $_POST[$value] = " - ";
            }
        }
        $param = array
            (
            'pat_right' => $this->input->post('pat_right'),
            'pat_name' => $this->input->post('pat_name'),
            'pat_lname' => $this->input->post('pat_lname'),
            'pat_birth' => $this->input->post('pat_birth'),
            'pat_year' => $this->input->post('pat_year'),
            'pat_sex' => $this->input->post('pat_sex'),
            'pat_status' => $this->input->post('pat_status'),
            'pat_nationality' => $this->input->post('pat_nationality'),
            'pat_race' => $this->input->post('pat_race'),
            'pat_raligion' => $this->input->post('pat_raligion'),
            'pat_id_card' => $this->input->post('pat_id_card'),
            'pat_address_local' => '<b>เลขที่</b>..... ' . $this->input->post('no') . ' .....<b>หมู่ที่</b>..... ' . $this->input->post('moo') . '   .....<b>ชื่อหมูบ้าน</b>..... ' . $this->input->post('house') . ' .....<b>ถนน</b>..... ' . $this->input->post('road') . '  .....<b>ซอย</b>..... ' . $this->input->post('street') . ' .....<br><b>ตำบล</b>..... ' . $this->input->post('tombol') . ' .....<b>อำเภอ</b>..... ' . $this->input->post('aumper') . ' <br>.....<b>จังหวัด</b>..... ' . $this->input->post('j_province') . ' .....<b>รหัสไปรษณีย์</b>..... ' . $this->input->post('zipcode') . '  .....<b>ประเทศ</b>.....  ' . $this->input->post('country'),
            'pat_tel_local' => $this->input->post('pat_tel_local'),
            'pat_mobile_local' => $this->input->post('pat_mobile_local'),
            'pat_address_current' => '<b>เลขที่</b>.....' . $this->input->post('c_no') . ' .....<b>หมู่ที่</b>..... ' . $this->input->post('c_moo') . '   .....<b>ชื่อหมูบ้าน</b>.....  ' . $this->input->post('c_house') . ' .....<b>ถนน</b>.....  ' . $this->input->post('c_road') . '  .....<b>ซอย</b>.....  ' . $this->input->post('c_street') . ' .....<br><b>ตำบล</b>.....  ' . $this->input->post('c_tombol') . ' .....<b>อำเภอ</b>.....  ' . $this->input->post('c_aumper') . '<br> .....<b>จังหวัด</b>.....  ' . $this->input->post('c__province') . ' .....<b>รหัสไปรษณีย์</b>.....  ' . $this->input->post('c_zipcode') . '  .....<b>ประเทศ</b>.....  ' . $this->input->post('c_country'),
            'pat_tel_current' => $this->input->post('pat_tel_current'),
            'pat_mobile_current' => $this->input->post('pat_mobile_current'),
            'pat_contact_name' => $this->input->post('pat_contact_name'),
            'pat_contact_lname' => $this->input->post('pat_contact_lname'),
            'pat_contact_rel' => $this->input->post('pat_contact_rel'),
            'pat_contact_tel' => $this->input->post('pat_contact_tel'),
            'pat_contact_mobile' => $this->input->post('pat_contact_mobile'),
            'pat_job_before' => $this->input->post('pat_job_before'),
            'pat_job_current' => $this->input->post('pat_job_current'),
            'pat_cause' => $this->input->post('pat_cause'),
            'pat_date_amp_left' => $this->input->post('pat_date_amp_left'),
            'pat_date_amp_right' => $this->input->post('pat_date_amp_right'),
            'amp_type_other' => $this->input->post('amp_type_other'),
            'pat_pros_place_radio' => $this->input->post('pat_pros_place_radio'),
            'pat_pros_place' => $this->input->post('pat_pros_place'),
            'pat_pros_date' => $this->input->post('pat_pros_date'),
            'pat_move_ment' => $this->input->post('pat_move_ment'),
            'pat_move_ment_tool' => $this->input->post('pat_move_ment_tool'),
        );

        if ($this->input->post('skip') == 1) {
            unset($param['pat_address_current']);
            $param['pat_address_current'] = $param['pat_address_local'];
        }




        $this->general_model->insert('patients', $param);
        $pat_id = $this->db->insert_id();
        $data_adr_local = array
            (
            'pat_id' => $pat_id,
            'no' => $this->input->post('no'),
            'moo' => $this->input->post('moo'),
            'house' => $this->input->post('house'),
            'road' => $this->input->post('road'),
            'street' => $this->input->post('street'),
            'tombol' => $this->input->post('tombol'),
            'aumper' => $this->input->post('aumper'),
            'province' => $this->input->post('j_province'),
            'zipcode' => $this->input->post('zipcode'),
            'country' => $this->input->post('c_country'),
            'ads_type' => 1,
        );
        $data_adr_current = array
            (
            'pat_id' => $pat_id,
            'no' => $this->input->post('c_no'),
            'moo' => $this->input->post('c_moo'),
            'house' => $this->input->post('c_house'),
            'road' => $this->input->post('c_road'),
            'street' => $this->input->post('c_street'),
            'tombol' => $this->input->post('c_tombol'),
            'aumper' => $this->input->post('c_aumper'),
            'province' => $this->input->post('c_j_province'),
            'zipcode' => $this->input->post('c_zipcode'),
            'country' => $this->input->post('c_country'),
            'ads_type' => 2,
        );

        if ($this->input->post('skip') == 1) {
            $data_adr_current = $data_adr_local;
            unset($data_adr_current['ads_type']);
            $data_adr_current['ads_type'] = 2;
        }
        $this->general_model->insert('adress', $data_adr_local);
        $this->general_model->insert('adress', $data_adr_current);
    }

    public function add_service($pat_id, $check_id = false) {

        $data['patient'] = $this->general_model->get_row('patients', "pat_id = {$pat_id}");
        $data['place'] = $this->general_model->get_field('distinct(place)', 'check_pat');
        $data['pat_right'] = $this->general_model->get_field('distinct(pat_right)', 'check_pat', 'pat_right != "" ');
        if ($check_id != false) {
            $data['check_pat'] = $this->general_model->get_row('check_pat', "check_id = {$check_id}");
        } else {
            $data['check_pat'] = false;
        }


        parent::core_staff_view('staff/add_service', $data);
    }

    public function query_service($id = false) {

        $param = array
            (
            'pat_id' => $this->input->post('pat_id'),
            'place' => $this->input->post('place'),
            'province' => $this->input->post('province'),
            'date' => $this->input->post('date'),
            'pat_right' => $this->input->post('pat_right'),
            'check_status' => 'เริ่มบริการ รอการตรวจร่างกาย',
        );

        if ($id == false) {
            $this->general_model->insert('check_pat', $param);
        } else {
            $this->general_model->update('check_pat', $param, "check_id ={$id}");
        }
        redirect(base_url() . 'patient/list_pat_check');
    }

    function list_pat($page = false) {
        /*         * pagination */
        $row = $this->general_model->get('patients', '', '', '', '', 'count', 'status != "10"');
        $url = base_url() . 'patient/index/';
        parent::pagination($page, $url, $row, $this->per_page, 3);
        /*         * pagination */
        $data['patient'] = $this->general_model->get('patients', $this->start_no, $this->per_page, 'date', $group_by = false, '', 'status !="10" ');

        // echo $this->db->last_query();die();
        $view = 'core_' . $this->usertype . '_view';
        parent::$view('staff/list_pat', $data);
    }

}
